package patterns.principles;

/**
 * 依赖倒置原则(Dependency Inversion Principle, DIP)示例
 * 高层模块不应该依赖低层模块，两者都应该依赖抽象
 * 抽象不应该依赖细节，细节应该依赖抽象
 */
public class DependencyInversionPrinciple {

    public static void main(String[] args) {
        // 不符合DIP的例子
        System.out.println("===== 不符合DIP的例子 =====");
        LightBulbBad bulb = new LightBulbBad();
        SwitchBad switchBad = new SwitchBad(bulb);
        
        switchBad.turnOn();
        switchBad.turnOff();
        
        // 如果要使用风扇，需要修改Switch类
        FanBad fanBad = new FanBad();
        SwitchBadForFan switchBadForFan = new SwitchBadForFan(fanBad);
        
        switchBadForFan.turnOn();
        switchBadForFan.turnOff();
        
        // 符合DIP的例子
        System.out.println("\n===== 符合DIP的例子 =====");
        // 使用灯泡
        Device lightBulb = new LightBulb();
        Switch goodSwitch = new Switch(lightBulb);
        
        goodSwitch.turnOn();
        goodSwitch.turnOff();
        
        // 使用风扇，无需修改Switch类
        Device fanDevice = new Fan();
        Switch fanSwitch = new Switch(fanDevice);
        
        fanSwitch.turnOn();
        fanSwitch.turnOff();
        
        // 使用空调，同样无需修改Switch类
        Device ac = new AirConditioner();
        Switch acSwitch = new Switch(ac);
        
        acSwitch.turnOn();
        acSwitch.turnOff();
    }
}

/**
 * 不符合依赖倒置原则的实现
 */
class LightBulbBad {
    public void turnOn() {
        System.out.println("灯泡打开");
    }
    
    public void turnOff() {
        System.out.println("灯泡关闭");
    }
}

class FanBad {
    public void turnOn() {
        System.out.println("风扇打开");
    }
    
    public void turnOff() {
        System.out.println("风扇关闭");
    }
}

// 开关直接依赖于灯泡
class SwitchBad {
    private LightBulbBad bulb;
    
    public SwitchBad(LightBulbBad bulb) {
        this.bulb = bulb;
    }
    
    public void turnOn() {
        bulb.turnOn();
    }
    
    public void turnOff() {
        bulb.turnOff();
    }
}

// 为了支持风扇，需要创建新的开关类
class SwitchBadForFan {
    private FanBad fan;
    
    public SwitchBadForFan(FanBad fan) {
        this.fan = fan;
    }
    
    public void turnOn() {
        fan.turnOn();
    }
    
    public void turnOff() {
        fan.turnOff();
    }
}

/**
 * 符合依赖倒置原则的实现
 */
// 抽象接口
interface Device {
    void turnOn();
    void turnOff();
}

// 具体实现：灯泡
class LightBulb implements Device {
    @Override
    public void turnOn() {
        System.out.println("灯泡打开");
    }
    
    @Override
    public void turnOff() {
        System.out.println("灯泡关闭");
    }
}

// 具体实现：风扇
class Fan implements Device {
    @Override
    public void turnOn() {
        System.out.println("风扇打开");
    }
    
    @Override
    public void turnOff() {
        System.out.println("风扇关闭");
    }
}

// 具体实现：空调
class AirConditioner implements Device {
    @Override
    public void turnOn() {
        System.out.println("空调打开");
    }
    
    @Override
    public void turnOff() {
        System.out.println("空调关闭");
    }
}

// 开关依赖于抽象接口，而不是具体实现
class Switch {
    private Device device;
    
    public Switch(Device device) {
        this.device = device;
    }
    
    public void turnOn() {
        device.turnOn();
    }
    
    public void turnOff() {
        device.turnOff();
    }
} 